﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CloudTaobao.Util;
using System.Windows.Media.Imaging;

namespace CloudTaobao
{

    public partial class MainFunctionPage : PhoneApplicationPage
    {
        Find fd = new Find();
        bool isRefreshTaoke = true;

        public MainFunctionPage()
        {
            InitializeComponent();
            fd.startLoadingEvent += new StartLoadingEventHandler(fd_startLoadingEvent);
            fd.endLoadingEvent += new EndLoadingEventHandler(fd_endLoadingEvent);
        }

        //navigate to the start page
        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            //fo stuff here, forgot about base.OnBackKeyPress(e);
            Utils.Quit();
        }

        void fd_endLoadingEvent()
        {
            loading.IsIndeterminate = false;
            loading.Visibility = Visibility.Collapsed;

            if(fd.taokeItemList.Count==0)
                return;

            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                taokeGrid.Visibility = Visibility.Visible;
                for (int i = 0; i != fd.taokeItemList.Count; i++) 
                {
                    switch (i) 
                    {
                        case 0:
                            img1.Source = new BitmapImage(new Uri(fd.taokeItemList.ElementAt<Item>(0).pic_url, UriKind.RelativeOrAbsolute));
                            price1.Text = fd.taokeItemList.ElementAt<Item>(0).price;
                            break;
                        case 1:
                            img2.Source = new BitmapImage(new Uri(fd.taokeItemList.ElementAt<Item>(1).pic_url, UriKind.RelativeOrAbsolute));
                            price2.Text = fd.taokeItemList.ElementAt<Item>(1).price;
                            break;
                        case 2:
                            img3.Source = new BitmapImage(new Uri(fd.taokeItemList.ElementAt<Item>(2).pic_url, UriKind.RelativeOrAbsolute));
                            price3.Text = fd.taokeItemList.ElementAt<Item>(2).price;
                            break;
                        case 3:
                            img4.Source = new BitmapImage(new Uri(fd.taokeItemList.ElementAt<Item>(3).pic_url, UriKind.RelativeOrAbsolute));
                            price4.Text = fd.taokeItemList.ElementAt<Item>(3).price;
                            break;
                    }
                }
                isRefreshTaoke = false;
            });
        }

        void fd_startLoadingEvent(object source, StartLoadingEventArgs args)
        {
            loading.IsIndeterminate = true;
            loading.Visibility = Visibility.Visible;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            App app = Application.Current as App;
            AppSettings settings = new AppSettings();
            //app.sessionInfo = this.NavigationContext.QueryString;           

            if (settings.valid == false)
            {
                //NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
                log.Content = "登录";
                nickName.Text = "";
                base.OnNavigatedTo(e);
            }
            else
            {
                log.Content = "退出";

                base.OnNavigatedTo(e);
                nickName.Text = settings.nick;
            }
        }

        private void find_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/FindPage.xaml", UriKind.Relative));
        }

        private void trade_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/TradePage.xaml", UriKind.Relative));
        }

        private void wangwang_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/WangWangPage.xaml", UriKind.Relative));
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                App app = Application.Current as App;
                if (app.sessionInfo != null)
                {
                    app.sessionInfo.Clear();
                    //app.sessionInfo = null;
                }

                AppSettings settings = new AppSettings();
                settings.valid = false;
                settings.nick = "";
                settings.top_session = "";
                settings.token = "";
                if (log.Content.Equals("登录"))
                {
                    if (!Microsoft.Phone.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
                        throw new NetException();
                    NavigationService.Navigate(new Uri("/LoginPage.xaml", UriKind.Relative));
                }
                else
                {
                    nickName.Text = "";
                    log.Content = "登录";
                }
            }
            catch (NetException ex) 
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void Settings_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Settings.xaml", UriKind.Relative));
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            AppSettings settings = new AppSettings();
            if (settings.first)
            {
                MessageBox.Show("本客户端由 沈雁俊开发并承担全部责任。您可以通过本客户端登录淘宝网使用淘宝网服务，但淘宝网及其所有人（经营人）无需就本客户端向您承担任何责任", "提示", MessageBoxButton.OK);
                settings.first = false;
            }
            isRefreshTaoke = true;
        }

        private void Panorama_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (panorama.SelectedIndex == 1)
            {
                if (!isRefreshTaoke)
                    return;
                try
                {
                    fd.GetTaokeItemList(4);
                    //AdTool ad = new AdTool();
                    //ad.getAdContent();
                }
                catch(Exception ex) 
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            UriBuilder uri = new UriBuilder();
            uri.Query = "targetUrl=" + fd.taokeItemList.ElementAt<Item>(0).wap_detail_url;
            NavigationService.Navigate(new Uri("/WebBrowserPage.xaml" + uri.Query, UriKind.Relative));
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            UriBuilder uri = new UriBuilder();
            uri.Query = "targetUrl=" + fd.taokeItemList.ElementAt<Item>(1).wap_detail_url;
            NavigationService.Navigate(new Uri("/WebBrowserPage.xaml" + uri.Query, UriKind.Relative));
        }

        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            UriBuilder uri = new UriBuilder();
            uri.Query = "targetUrl=" + fd.taokeItemList.ElementAt<Item>(2).wap_detail_url;
            NavigationService.Navigate(new Uri("/WebBrowserPage.xaml" + uri.Query, UriKind.Relative));
        }

        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            UriBuilder uri = new UriBuilder();
            uri.Query = "targetUrl=" + fd.taokeItemList.ElementAt<Item>(3).wap_detail_url;
            NavigationService.Navigate(new Uri("/WebBrowserPage.xaml" + uri.Query, UriKind.Relative));
        }
    }
}